package com.fiap.activities;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fiap.dao.UserOperations;
import com.fiap.functions.UserFunctions;

public class LoginActivity extends Activity {
	
	private TextView registerScreen;
	private EditText txtEmail;
	private EditText txtPassword;
	private Button btnLogin;
	private TextView errorMessage;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        prepareScreen();
        
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// Switching to Register screen
				Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
				startActivity(intent);
			}
		});
        
        btnLogin.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				String email = txtEmail.getText().toString();
                String password = txtPassword.getText().toString();
                if(!(validateEmail(email) && validateEmpty(password))) {
                	
                }
                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.loginUser(email, password);
				
                try {
                    if (json.getString("success") != null) {
                        String res = json.getString("success"); 
                        if(Integer.parseInt(res) == 1){
                            // user successfully logged in
                            // Store user details in SQLite Database
                            UserOperations userOperations = new UserOperations(getApplicationContext());
                            JSONObject json_user = json.getJSONObject("user");
                             
                            // Clear all previous data in database
//                            userFunction.logoutUser(getApplicationContext());
                            userOperations.insertUser(json_user.getLong("id"), json_user.getString("username"), 
                            		json_user.getString("email"));                        
                             
                            // Launch main menu Screen
                            Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
                            // Close all views before launching Dashboard
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);
                            finish();
						} else {
							//Show error message
							errorMessage.setVisibility(0);
						}
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
			}

		});
        
    }
    
    protected boolean validateEmpty(String password) {
		// TODO Auto-generated method stub
		return false;
	}

	protected boolean validateEmail(String email) {
		// TODO Auto-generated method stub
		return false;
	}

	private void prepareScreen() {
    	txtEmail = (EditText) findViewById(R.id.txtEmail);
    	txtPassword = (EditText) findViewById(R.id.txtPassword);
    	btnLogin = (Button) findViewById(R.id.btnLogin);
    	registerScreen = (TextView) findViewById(R.id.link_to_register);
    	errorMessage = (TextView) findViewById(R.id.txtErrorMessage);
	}

}